//
//  OrderShowViewController.m
//  JiongTu
//
//  Created by Dupengfei on 16/3/17.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "OrderShowViewController.h"
#include "OrderTableViewCell.h"
#import "OrderDetailedViewController.h"

#import "JTDriverTrip.h"
#import "JTPassengerTrip.h"
#import "JTUser.h"
#import "JTConst.h"

#import <AVOSCloud.h>
#import <SVProgressHUD.h>

@interface OrderShowViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray * array;

@property (nonatomic, strong)NSMutableArray * headImage;

@property (nonatomic, assign)int driverseat;
@property (nonatomic, assign)int passengerseat;

@end

@implementation OrderShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeInterface];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [SVProgressHUD dismiss];
}

- (void)setOneTrip:(JTPassengerTrip *)oneTrip {
    _oneTrip = oneTrip;
    
    
}
//界面
-(void)initializeInterface
{
    self.title = @"订单匹配";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(dismissClick:)];
}

-(void)viewWillAppear:(BOOL)animated
{
[self initializeDateSource];
}

//数据
-(void)initializeDateSource
{   _array =[NSMutableArray array];
    NSDictionary * dic =@{@"time":_oneTrip.time,@"start":_oneTrip.start,@"end":_oneTrip.end};
    [SVProgressHUD showWithStatus:@"查询中"];
    [AVCloud callFunctionInBackground:@"test" withParameters:dic block:^(id object, NSError *error) {
        _array = [JTDriverTrip tripWithAVObjects:object];
        if (!error && _array.count) {
            [SVProgressHUD dismiss];
            [_tableView reloadData];
        }
        else if (!error && !_array.count) {
            [SVProgressHUD showInfoWithStatus:@"暂无该方向的车辆，请到订单中查询"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        } else if (error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        }
       
        
    }];
}



#pragma mark-delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"OrderCell"];
    
    if (!cell) {
        cell =[[[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:nil options:nil] lastObject];
    }
    
    JTDriverTrip * trip =_array[indexPath.row];
    

    
    cell.oneDriverTrip = trip;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 95;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    JTDriverTrip * trip =_array[indexPath.row];
    
    _driverseat =[trip.seatCount intValue]-[trip.currentSeatCount intValue];
    
    _passengerseat =[_oneTrip.orderSeatNumber intValue];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (_passengerseat>_driverseat) {
        
        [SVProgressHUD showErrorWithStatus:@"所选车辆剩余座位与您的需求不符，请选择其他车辆"];
        
    }
    else
    {
    OrderDetailedViewController * view =[[OrderDetailedViewController alloc]init];
    
    JTDriverTrip * trip =_array[indexPath.row];
   
    view.driverTrip =trip;
    
    
    [self.navigationController pushViewController:view animated:YES];
    }
}

#pragma mark - 点击事件
- (void)dismissClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:tmpPassTripID];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:tmpPassTripSeats];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }];
}


@end
